package cn.springcloud.alibaba.admin.config;

import cn.springcloud.alibaba.security.handler.MyAccessDeniedHandler;
import cn.springcloud.alibaba.security.handler.MyAuthenticationEntryPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;

@Configuration
@EnableResourceServer
public class ResourceConfig extends ResourceServerConfigurerAdapter {

	public static final String RESOURCE_ID = "admin-oauth2-resource";

	@Autowired
	private MyAccessDeniedHandler myAccessDeniedHandler;
	@Autowired
	private MyAuthenticationEntryPoint myAuthenticationEntryPoint;

	@Override
	public void configure(HttpSecurity http) throws Exception {
		http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED);
		http.authorizeRequests().anyRequest().authenticated();
	}

	@Override
	public void configure(ResourceServerSecurityConfigurer resources) {
		resources
			.resourceId(RESOURCE_ID)
			.accessDeniedHandler(myAccessDeniedHandler)
			.authenticationEntryPoint(myAuthenticationEntryPoint)
			.stateless(true);
	}
}
